const app = getApp()
import { searchCompany } from '../../../api/index'

Page({
  data: {
    list: [],
    safeBottom: 0,
    keyword: ''
  },
  next () {
    let isApprove = 1
    if (!this.data.keyword) {
      wx.showToast({ title: '请先输入要认证的企业名称！', icon: 'none' })
      return
    }
    if (this.data.list.length) {
      let index = this.data.list.findIndex(item => item.companyName === this.data.keyword)
      if (index > -1) isApprove = 2
      else isApprove = 1
    } else {
      isApprove = 1
    }
    wx.$nav.navigateTo(`/pages/account/editApprove/index?isApprove=${isApprove}&companyName=${this.data.keyword}`)
  },
  async search ({ detail }) {
    try {
      this.setData({ keyword: detail })
      const { ResultSet } = await searchCompany({ companyName: detail })
      this.setData({ list: ResultSet.userData.data })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad () {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    this.setData({ safeBottom: screenHeight - bottom })
  }
})